package main

import (
    "time"
    "os"
    "fmt"
    // "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/app"
    "fyne.io/fyne/v2/widget"
    "gorm.io/driver/sqlite"
    "gorm.io/gorm"
)
type User struct {
    ID        uint      `json:"id"`
    Name      string    `json:"Name"`
    Email     string    `json:"Email"`
    Createdat time.Time `json:"Createdat"`
    Updatedat time.Time `json:"Updatedat"`
}
// type User struct {
//     gorm.Model
//     ID        uint      `json:"id"`
//     Name      string    `json:"Name"`
//     Email     string    `json:"Email"`
//     Createdat time.Time `json:"Createdat"`
//     Updatedat time.Time `json:"Updatedat"`
// }
func createUser() {
    mydir, err := os.Getwd()
    if err != nil {
        panic("failed to Getwd")
        fmt.Println(err)
    }
    fmt.Println(mydir)
    db, err := gorm.Open(sqlite.Open(mydir+"/data.sqlite"), &gorm.Config{})
    if err != nil {
        panic("failed to connect database")
    }

    var c time.Time = time.Now()
    var u time.Time = time.Now()
    db.Create(&User{Name: "D42", Email: "m@sina.com", Createdat: c, Updatedat: u})
}
func main() {
    a := app.New()
    w := a.NewWindow("Clock")

    clock:=widget.NewButton("new user",createUser2)
    w.SetContent(clock) 
    w.Show()

    a.Run()
}
// func input(display *widget.Entry,value string) func(){
//     return func(){
//         display.Text+=value
//         display.Refresh()
//     }
// }
func createUser2(){
    fmt.Println("createUser2")
    mydir, err := os.Getwd()
    if err != nil {
        panic("failed to Getwd")
        fmt.Println(err)
    }
    fmt.Println(mydir)
    db, err := gorm.Open(sqlite.Open(mydir+"/data.sqlite"), &gorm.Config{})
    if err != nil {
        panic("failed to connect database")
    }

    // 迁移 schema
    // db.AutoMigrate(&User{})

    // Create
    var c time.Time = time.Now()
    var u time.Time = time.Now()
    db.Create(&User{Name: "D42", Email: "m@sina.com", Createdat: c, Updatedat: u})
}